Acala

Row

Overall Total Value Locked (TVL)

  • The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.Acala.network page.
Date Block dexTVL bridgeTVL liquidStakingTVL crowdloanTVL lcdotTVL stableCoinTVL OverallTVL
2022-02-10 382,658 0 4,166,200,000 0.00 677,340,384 502,363,724 7,341,784 4,675,905,508
2022-02-11 389,478 16,094,236 3,783,000,000 0.00 615,039,766 456,157,162 17,382,712 4,272,634,110
2022-02-12 396,298 17,016,890 3,766,800,000 0.00 612,405,972 454,203,753 18,165,975 4,256,186,617
2022-02-13 403,080 16,954,608 3,740,600,000 0.00 608,146,378 451,044,536 18,378,380 4,226,977,524
2022-02-14 409,851 16,790,368 3,752,200,000 0.00 610,032,305 452,443,273 18,606,109 4,240,039,750
2022-02-15 416,618 17,201,570 4,027,800,000 0.00 654,839,326 485,675,341 22,576,976 4,553,253,888
2022-02-16 423,364 19,156,534 3,994,800,000 0.00 649,474,189 481,696,175 25,420,112 4,521,072,821
2022-02-17 429,431 18,765,765 3,633,800,000 0.00 590,782,845 438,166,507 24,404,858 4,115,137,130
2022-02-18 435,379 18,569,217 3,564,400,000 0.00 579,499,800 429,798,199 24,337,215 4,037,104,631
2022-02-19 441,244 18,878,530 3,567,600,000 0.00 580,020,055 430,184,058 24,803,661 4,041,466,249
2022-02-20 447,046 19,023,121 3,426,800,000 0.00 557,128,805 413,206,281 30,189,557 3,889,218,960
2022-02-21 452,805 20,067,041 3,249,000,000 0.00 528,222,099 391,767,015 30,621,759 3,691,455,815
2022-02-22 458,591 19,701,249 3,265,400,000 0.00 530,888,409 393,744,540 30,963,722 3,709,809,511
2022-02-23 464,375 19,537,419 3,212,600,000 0.00 522,304,190 387,377,874 30,455,664 3,649,970,957
2022-02-24 470,299 18,494,709 3,193,200,000 0.00 519,150,140 385,038,607 30,264,104 3,626,997,420
2022-02-25 476,519 19,319,181 3,448,400,000 0.00 560,640,531 415,810,826 32,588,262 3,916,118,269
2022-02-26 483,026 19,450,986 3,600,600,000 0.00 585,385,192 434,163,224 34,170,399 4,088,384,609
2022-02-27 489,670 19,361,671 3,486,200,000 0.00 566,786,051 420,368,781 33,081,046 3,959,011,498
2022-02-28 496,214 19,375,345 3,758,400,000 0.00 611,040,301 453,190,874 35,851,414 4,266,817,633
2022-03-01 502,741 19,797,112 37,738,000 0.00 613,544,031 455,047,818 36,068,245 548,651,176
2022-03-02 509,328 19,714,554 37,294,000 0.00 606,325,483 449,694,031 35,875,337 542,577,922
2022-03-03 515,915 21,601,531 36,096,000 0.00 586,848,411 435,248,451 62,551,692 555,497,675
2022-03-04 522,577 21,109,933 33,526,000 0.00 545,065,377 404,259,186 62,448,822 521,343,941
2022-03-05 529,288 21,046,495 34,594,000 0.00 562,428,910 417,137,215 65,629,199 538,406,909
2022-03-06 535,942 21,517,523 34,130,000 0.00 554,885,203 411,542,266 64,244,858 531,434,647
2022-03-07 542,760 20,698,881 32,756,000 0.00 532,546,724 394,974,464 61,806,180 510,235,526
2022-03-08 549,653 21,426,517 33,786,000 0.00 549,292,454 407,394,287 62,804,874 525,411,678
2022-03-09 556,567 22,187,048 35,432,000 0.00 576,053,106 427,241,886 67,566,345 552,427,279
2022-03-10 563,674 21,797,345 34,072,000 0.00 553,942,239 410,842,898 64,484,996 531,197,238
2022-03-11 570,668 22,623,490 35,698,000 0.00 580,377,731 430,449,335 65,532,242 554,303,068
2022-03-12 577,693 23,152,676 36,542,000 0.00 594,099,475 440,626,355 67,263,520 567,584,551
2022-03-13 584,752 22,686,501 34,726,000 0.00 564,574,965 418,728,882 64,046,341 540,187,724
2022-03-14 591,860 22,701,522 35,342,000 0.00 574,589,887 426,156,659 69,433,042 553,633,224
2022-03-15 598,871 23,036,986 35,672,000 0.00 579,955,023 430,135,825 67,957,199 556,802,010
2022-03-16 605,882 23,921,422 37,984,000 0.00 617,543,497 458,014,106 71,306,499 591,226,027
2022-03-17 612,917 22,161,063 37,460,000 0.00 609,024,310 451,695,672 70,199,287 581,516,023
2022-03-18 620,006 22,438,611 38,024,000 0.00 618,193,816 458,496,429 72,350,849 591,309,890
2022-03-19 627,056 22,353,285 38,728,000 0.00 629,639,441 466,985,317 75,180,260 603,246,863
2022-03-20 634,100 22,073,101 37,390,000 0.00 607,886,250 450,851,607 73,690,440 584,005,147
2022-03-21 641,175 22,291,249 37,496,000 0.00 609,609,597 452,129,763 77,140,785 589,057,797
2022-03-22 648,244 23,278,446 40,526,000 0.00 658,871,307 488,665,745 78,792,353 631,262,544
2022-03-23 655,070 23,461,611 41,664,000 0.00 677,372,900 502,387,840 81,244,807 648,758,258
2022-03-24 661,932 21,153,952 42,586,000 0.00 692,362,767 513,505,390 83,431,449 660,676,791
2022-03-25 668,922 21,306,124 41,092,000 0.00 668,073,330 495,490,618 79,385,632 637,274,374
2022-03-26 675,883 21,542,172 41,658,000 0.00 677,275,352 502,315,491 82,516,037 648,031,701
2022-03-27 682,919 23,079,423 44,528,000 0.00 723,935,784 536,922,181 86,241,201 690,770,804
2022-03-28 689,974 23,409,978 44,534,000 0.00 724,033,332 536,994,529 87,643,336 692,581,844
2022-03-29 697,050 23,591,321 44,294,000 0.00 720,131,414 534,100,590 89,331,651 691,317,562
2022-03-30 704,068 23,857,058 45,036,000 0.00 732,194,843 543,047,685 92,283,577 704,224,320
2022-03-31 711,104 23,395,737 42,972,000 0.00 698,638,351 518,159,808 96,421,301 680,948,847
2022-04-01 718,140 23,701,108 43,878,000 0.00 713,368,090 529,084,429 104,794,678 701,458,215
2022-04-02 725,135 24,887,333 45,910,000 0.00 746,404,326 553,586,447 110,536,827 734,920,607
2022-04-03 732,169 24,976,394 46,504,000 0.00 756,061,572 560,748,947 109,398,237 741,627,578
2022-04-04 739,232 24,915,365 45,238,000 0.00 735,478,957 545,483,417 114,607,746 730,244,529
2022-04-05 746,312 24,464,193 44,110,000 0.00 717,139,944 531,881,903 112,332,392 712,788,488
2022-04-06 753,339 23,335,329 39,880,000 0.00 648,368,646 480,876,225 98,378,537 642,470,091
2022-04-07 760,400 23,441,333 40,698,000 0.00 661,667,682 490,739,735 104,512,956 659,392,023
2022-04-08 767,454 23,329,475 38,616,000 0.00 627,818,546 465,634,812 97,358,875 624,939,163
2022-04-09 774,447 23,552,480 39,236,000 0.00 637,898,500 473,110,822 102,638,645 638,537,947
2022-04-10 781,545 23,609,472 39,172,000 0.00 636,857,989 472,339,105 108,925,810 644,046,386
2022-04-11 788,553 22,497,741 34,518,000 0.00 561,193,303 416,220,801 95,815,282 569,051,825
2022-04-12 795,620 22,429,761 35,566,000 0.00 578,231,676 428,857,669 95,523,923 582,377,353
2022-04-13 802,608 22,989,411 36,390,000 0.00 591,628,260 438,793,527 99,176,405 597,349,342
2022-04-14 809,644 23,399,894 35,830,000 0.00 582,523,786 432,041,002 96,065,859 587,336,755
2022-04-15 816,668 23,280,552 36,614,000 0.00 595,270,050 441,494,537 98,171,487 599,560,576
2022-04-16 823,732 23,117,546 37,270,000 0.00 605,935,292 449,404,637 103,152,932 612,945,116
2022-04-17 830,766 23,167,619 35,584,000 0.00 578,524,320 429,074,714 98,611,872 586,438,205
2022-04-18 837,826 23,230,902 36,382,000 0.00 591,498,196 438,697,062 95,405,181 593,715,145
2022-04-19 844,870 23,931,360 37,750,000 0.00 613,739,127 455,192,515 98,350,575 615,224,451
2022-04-20 851,796 24,071,604 38,196,000 0.00 620,990,190 460,570,419 97,073,865 619,911,887
2022-04-21 858,830 23,832,440 36,368,000 0.00 591,270,585 438,528,249 94,872,285 593,600,974
2022-04-22 865,842 24,221,077 36,652,000 0.00 595,887,854 441,952,744 92,765,978 595,591,799
2022-04-23 872,830 24,054,342 37,652,000 0.00 612,145,844 454,010,823 91,038,073 606,755,239
2022-04-24 879,807 23,898,657 36,488,000 0.00 593,221,543 439,975,218 85,704,047 586,065,923
2022-04-25 886,715 23,237,058 36,294,000 0.00 590,067,493 437,635,951 86,257,029 583,424,039
2022-04-26 893,713 22,897,882 33,242,000 0.00 540,448,107 400,834,691 79,312,452 536,287,025
2022-04-27 900,680 22,972,638 33,832,000 0.00 550,040,322 407,948,958 80,635,836 545,389,432
2022-04-28 907,463 22,683,419 33,962,000 0.00 552,153,860 409,516,509 82,101,429 548,263,357
2022-04-29 914,420 21,562,000 32,254,000 0.00 524,385,213 388,921,308 76,937,998 519,675,306
2022-04-30 921,375 20,113,100 29,036,000 0.00 472,067,001 350,118,407 70,866,527 470,134,035
2022-05-01 928,262 20,315,368 30,704,000 0.00 499,185,329 370,231,284 72,475,060 493,725,713
2022-05-02 935,210 20,209,506 29,986,000 0.00 487,512,092 361,573,583 70,661,182 482,430,271
2022-05-03 941,991 20,104,425 29,408,000 0.00 478,114,973 354,604,013 71,190,161 475,306,599
2022-05-04 948,773 20,989,523 32,496,000 0.00 528,319,647 391,839,364 79,907,714 525,232,601
2022-05-05 955,553 19,749,098 28,805,352 0.00 472,424,677 350,383,685 70,122,361 469,060,496
2022-05-06 962,314 19,359,434 28,321,848 18,709.66 465,953,997 345,584,569 69,951,596 463,236,157
2022-05-07 969,145 18,932,729 27,318,979 18,116.83 447,289,824 331,741,894 95,920,777 473,932,495
2022-05-08 975,941 17,839,603 26,411,239 17,508.28 432,104,861 320,479,647 89,520,323 454,268,319
2022-05-09 982,741 14,690,241 21,843,369 14,556.12 356,342,627 264,288,995 76,471,410 377,308,570
2022-05-10 989,571 13,635,935 33,971,118 10,573,849.39 366,422,581 271,765,004 80,548,328 410,494,235
2022-05-11 996,410 10,259,516 38,629,991 25,248,761.36 289,662,107 214,833,986 75,631,240 364,603,494
2022-05-12 1,003,195 9,581,486 43,357,586 29,452,097.77 280,460,084 208,009,113 75,134,974 365,535,256
2022-05-13 1,010,207 10,973,168 52,121,142 37,477,184.73 340,279,733 252,375,612 89,176,105 442,123,212
2022-05-14 1,017,179 11,331,616 57,833,930 42,070,697.82 371,267,462 275,358,312 98,681,018 485,275,574
2022-05-15 1,024,222 11,916,291 59,550,437 43,613,104.21 381,022,256 282,593,160 103,845,945 501,518,937
2022-05-16 1,031,247 11,070,042 54,657,331 40,172,009.19 347,303,185 257,584,702 95,712,866 459,196,951
2022-05-17 1,038,263 11,549,278 57,704,263 42,267,121.91 359,626,741 266,724,727 98,506,448 476,751,838
2022-05-18 1,044,985 11,505,734 51,225,561 37,631,336.97 313,434,540 232,465,310 85,493,128 418,321,070
2022-05-19 1,051,890 11,467,898 53,020,130 39,959,639.25 325,582,510 241,475,108 85,772,675 431,695,450
2022-05-20 1,058,796 11,246,893 52,024,722 39,615,645.46 316,347,972 234,626,118 84,278,962 421,792,341
2022-05-21 1,065,696 11,187,309 53,463,810 41,060,888.51 325,354,898 241,306,294 86,987,403 434,005,705
2022-05-22 1,072,674 11,445,206 55,276,934 42,003,535.27 331,370,355 245,767,784 89,726,681 444,220,140
2022-05-23 1,079,644 11,393,589 53,743,260 41,420,150.22 323,244,611 239,741,156 86,373,238 432,671,393
2022-05-24 1,086,605 11,650,983 57,192,387 44,612,795.50 331,500,418 245,864,249 92,542,067 451,862,481
2022-05-25 1,093,548 11,355,388 56,065,717 43,547,582.16 321,238,375 238,253,189 89,923,289 439,145,164
2022-05-26 1,100,467 10,677,864 52,429,410 41,107,060.91 300,376,122 222,780,261 83,262,272 410,256,867
2022-05-27 1,107,371 10,335,111 52,269,373 41,115,076.23 297,059,492 220,320,412 81,623,140 405,663,113
2022-05-28 1,114,253 10,864,004 55,301,110 43,749,969.61 314,331,981 233,130,916 85,553,245 428,599,245
2022-05-29 1,121,139 11,131,725 57,043,239 45,136,471.59 324,051,007 240,339,236 88,774,150 442,424,822
2022-05-30 1,128,033 11,732,215 60,325,098 47,756,791.78 342,263,208 253,846,698 97,769,331 471,430,133
2022-05-31 1,134,939 11,805,351 70,812,367 58,963,766.59 340,474,829 252,520,309 96,860,090 490,961,884
2022-06-01 1,141,677 11,244,679 64,626,723 53,678,288.38 308,339,286 228,686,308 87,774,716 446,010,714
2022-06-02 1,148,531 11,450,050 67,823,058 56,511,651.37 322,851,168 239,449,350 92,935,143 468,169,253
2022-06-03 1,155,361 11,086,517 63,949,101 53,381,288.38 304,756,025 226,028,707 86,257,482 440,703,095
2022-06-04 1,162,191 11,044,275 64,723,443 53,877,894.95 305,695,737 226,725,664 87,782,839 444,154,116
2022-06-05 1,168,945 11,222,834 64,752,319 54,202,573.86 305,464,873 226,554,440 87,558,038 444,290,204
2022-06-06 1,175,760 11,275,119 65,650,998 54,781,007.72 307,841,791 228,317,331 88,813,285 448,837,741
2022-06-07 1,182,503 11,038,517 64,450,608 53,779,095.36 299,810,344 222,360,639 87,255,444 438,884,303
2022-06-08 1,189,273 10,853,966 63,350,724 53,132,601.89 293,245,368 217,491,587 85,246,943 430,075,822
2022-06-09 1,196,022 10,910,543 65,489,021 54,610,401.78 299,664,022 222,252,117 86,688,056 439,950,138
2022-06-10 1,202,808 10,455,392 61,326,768 51,566,393.28 281,442,067 208,737,421 81,255,048 413,341,022
2022-06-11 1,209,560 9,902,483 57,142,418 48,054,853.09 261,847,937 194,205,023 75,615,459 384,920,236
2022-06-12 1,216,309 9,303,464 53,442,770 44,898,748.56 244,692,506 181,481,337 69,012,028 358,138,348
2022-06-13 1,222,967 8,759,431 49,026,318 41,109,628.30 224,441,554 166,461,793 64,169,610 329,526,780
2022-06-14 1,229,621 8,848,993 52,084,513 43,817,449.90 238,524,225 176,906,501 67,628,085 349,285,544
2022-06-15 1,236,320 9,764,121 59,915,095 50,421,654.82 274,265,790 203,414,984 75,271,580 398,787,435
2022-06-16 1,242,989 8,920,166 50,422,711 42,246,468.86 229,458,769 170,182,916 64,113,318 335,885,580
2022-06-17 1,249,647 9,157,904 52,399,845 43,888,309.03 238,029,982 176,539,936 65,960,543 347,946,537
2022-06-18 1,256,310 8,640,015 50,679,075 42,513,907.14 229,595,336 170,284,204 63,353,334 335,470,535
2022-06-19 1,263,003 8,885,295 54,052,727 45,131,272.59 243,544,692 180,630,037 66,981,819 355,681,151
2022-06-20 1,269,614 8,720,955 56,396,060 47,121,220.35 253,933,548 188,335,150 70,367,150 370,940,536
2022-06-21 1,276,376 8,808,088 56,013,858 46,820,285.73 251,904,550 186,830,301 70,704,699 369,177,232
2022-06-22 1,283,096 8,572,605 53,982,699 45,133,825.07 242,487,923 179,846,261 67,528,621 355,064,010
2022-06-23 1,289,783 8,728,083 56,210,393 47,089,747.52 252,525,606 187,290,920 70,574,437 369,893,580
2022-06-24 1,296,569 9,012,942 59,644,731 50,080,246.65 268,777,093 199,344,176 74,295,241 392,377,336
2022-06-25 1,303,311 8,936,434 58,659,973 49,338,164.37 264,016,753 195,813,571 72,511,923 385,260,065
2022-06-26 1,308,720 8,916,669 57,773,066 48,521,492.45 259,646,605 192,572,359 71,684,412 379,467,998

Row

Components of Total Value Locked

Row

Sources

Please click on the Source Code link at the top of the document to see the full source code. Here is a summary:

dexTVL

  • sum of tvlUSD from the xx function in the subscanr package for R. You can also use the Karura-dex project hosted on Subquery Network with this query:
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}

BridgeTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘KSM’}], block_hash = hash)
  • LKSM (or LDOT) obtained from function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘Token’: ‘LKSM’}], block_hash = hash)
  • both mulitplied by KSM (or DOT) price for the specific block number

liquidStakingTVL

  • KSM (or DOT) obtained from the py-substrate-interface using function substrate.query(module=‘Homa’,storage_function=‘TotalStakingBonded’, block_hash = hash)
  • mulitplied by KSM (or DOT) price for the specific block number

lcdotTVL (Acala only)

  • LCDOT obtained from the py-substrate-interface using function substrate.query(module=‘Tokens’,storage_function=‘TotalIssuance’, params = [{‘LiquidCrowdloan’: 13}], block_hash = hash)
  • mulitplied by DOT price for the specific block number

stableCoinTVL

{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
---
title: "Acala / Karura TVL Dashboard"
output:
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: scroll
    social: menu
    source_code: embed
params:
  network: Karura
  window: 7
  
---

```{css custom1, echo=FALSE}
.dataTables_scrollBody {
    max-height: 100% !important;
}
```

```{r global, include=FALSE}
library(knitr)
knitr::opts_chunk$set(
  message = FALSE,
  warning = FALSE,
  comment = "#>"
)

library(ggplot2)
# library(dygraphs)
library(kableExtra)
library(formattable)
library(lubridate)
library(flexdashboard)
library(DT)
library(subscanr)
library(formattable)
library(ghql)
x <- GraphqlClient$new()

# Helper function to concat
`%+%` <- function(a, b) paste0(a, b)

# window = params$window
window <- today() - as.Date("2021-12-31") + 1
network = params$network

# Liquidity Pool TVL ($3.72M) = totalDefiTvl = totalDexTvl + totalDexLockedTvl + homaLocked + stableCoinTvl
dex <- getDailyPools_acala_dex(network, window) %>%
  setorder(Date, pair)

dailyTVL <- dex[, .(max(updateAtBlock.id), sum(tvlUSD)), by = Date] %>%
  setnames(c("V1","V2"), c("Block","dexTVL"))

library(reticulate)
# use_python("/opt/homebrew/bin/python3.9")
```

```{python, include=FALSE}
from substrateinterface import SubstrateInterface

def getTotalIssuanceKSM():
    url = 'wss://karura.polkawallet.io'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
    ksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)
    lksm = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)
    result = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'KSM'}], block_hash = hash)
    price = result.value['value'] / 10**18
    
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    try:
      homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
      amount = 0
      for res in homa:
          amount += res[1].value['bonded']
    except Exception as e:
      amount = 0
      
    bridge = (float(ksm.value / 10**12) + float(lksm.value / 10**12 / 8.5)) * price
    liquidKSM = float(amount / 10**12) * price
    crowdloanTVL = (501137661910050505 / 10**12) * price
    return [bridge, liquidKSM, crowdloanTVL, block_id]

def getTotalIssuanceDOT():
    url = 'wss://acala-rpc-0.aca-api.network'
    substrate = SubstrateInterface(url)
    hash = substrate.get_block_hash(block_id)
    timestamp = substrate.query(module='Timestamp',storage_function='Now',block_hash=hash).value
    dot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'DOT'}], block_hash = hash)
    ldot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LDOT'}], block_hash = hash)
    lcdot = substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)
    result = substrate.query('AcalaOracle', 'Values', params = [{'Token': 'DOT'}], block_hash = hash)
    price = result.value['value'] / 10**18
    
    # homa = substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)
    homa = substrate.query_map(module='Homa',storage_function='StakingLedgers', block_hash = hash)
    amount = 0
    for res in homa:
        amount += res[1].value['bonded']
  
    bridge = (float(dot.value / 10**10) + float(ldot.value / 10**10 / 10)) * price
    liquidDOT = float(amount / 10**10) * price
    lcDOT = float(lcdot.value / 10**10) * price
    crowdloanTVL = (325159802323576263 / 10**10) * price
    return [bridge, liquidDOT, crowdloanTVL, lcDOT, block_id]

```



```{r tvl, cache = TRUE, include=FALSE}

# dailyTVL[, M := month(Date) %+% year(Date)]
# dailyTVL[, maxDate := max(Date), by = M]
# dailyTVL <- dailyTVL[Date == maxDate]

# LP Token Staking TVL = totalDexTvl = useTotalDexPoolTVL()
# totalDexLockedTvl = useTotalStaking() = api.query.dex?.liquidityPool
# KSM Bridge TVL = totalIssuanceKSM = useTotalIssuance('KSM') = api.query.tokens?.totalIssuance
dailyTVL[, bridgeTVL := 0]
# Liquid KSM TVL = totalLocked in Homa = useState()
dailyTVL[, liquidStakingTVL := 0]
# homeLocked = totalStaking * price
dailyTVL[, crowdloanTVL := 0]
if (tolower(network) == "acala") dailyTVL[, lcdotTVL := 0]

for (i in 1:nrow(dailyTVL)) {
  py_run_string("block_id = " %+% dailyTVL$Block[i])
  if (tolower(network) == "karura") {
    tmp = py$getTotalIssuanceKSM()
  } else {
    tmp = py$getTotalIssuanceDOT()
    dailyTVL$lcdotTVL[i] <- tmp[4]
  }
  dailyTVL$bridgeTVL[i] <- tmp[1]
  dailyTVL$liquidStakingTVL[i] <- tmp[2]
  dailyTVL$crowdloanTVL[i] <- tmp[3]
}

# stableCoinTvl
# const stableCoinTVL = useTotalLocked(); = api.query.loans?.totalPositions
collateral <- getLoansDailyCollateral_acala_loan(network, window, staging = FALSE) %>%
  setorder(Date, collateral.id)
stableCoinTvl <- collateral[, sum(depositVolumeUSD), by = Date] %>%
  setnames("V1", "stableCoinTVL")
dailyTVL <- merge(dailyTVL, stableCoinTvl, by = "Date", all.x = TRUE)

# If monthly, remove these fields
# try(dailyTVL[, M := NULL])
# try(dailyTVL[, maxDate := NULL])

# Make columns numeric
for (col in names(dailyTVL)[-1]) set(dailyTVL, j=col, value=as.numeric(dailyTVL[[col]])) 
if (tolower(network) == "acala") {
  dailyTVL[, OverallTVL := dexTVL + bridgeTVL + liquidStakingTVL + lcdotTVL + stableCoinTVL]
} else {
  dailyTVL[, OverallTVL := dexTVL + bridgeTVL + liquidStakingTVL + stableCoinTVL]
}

```

# `r network` {.tabset}

Row
----

### Overall Total Value Locked (TVL)

* The Overall TVL in the table below does not includes the Crowdloan TVL, which also seems to be the case on the stats.`r network`.network page.

```{r k_tvl}

knitr::kable(dailyTVL, escape = FALSE, format.args = list(big.mark = ",")) %>%
  kable_styling()

```

Row
----

### Components of Total Value Locked

```{r k_plot1}

if (tolower(network) == "acala") {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, bridgeTVL, liquidStakingTVL, lcdotTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'bridgeTVL', 'liquidStakingTVL', 'LCDOTTVL', 'stableCoinTVL')
} else {
  dat <- melt(data = dailyTVL[, .(Date, dexTVL, bridgeTVL, liquidStakingTVL, stableCoinTVL)], 
            id.vars = "Date",
            value.name = "TVL")
  cols <- c('dexTVL', 'bridgeTVL', 'liquidStakingTVL', 'stableCoinTVL')
  
}
dat[, TVL := TVL / 1e6]
 
# Stacked
ggplot(dat, aes(fill=variable, y=TVL, x=Date)) + 
    geom_bar(position="stack", stat="identity") +
    ggtitle(label="Components of Total Value Locked") + 
    ylab("Totval Value Locked (in millions USD)")

```


Row
----

### Sources

Please click on the *Source Code* link at the top of the document to see the full source code.  Here is a summary:

#### dexTVL 

- sum of tvlUSD from the xx function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-dex](https://explorer.subquery.network/subquery/AcalaNetwork/karura-dex) project hosted on Subquery Network with this query:
```
{ 
  dailyPools { 
    nodes { 
      timestamp token0 {id} token1 {id} feeRateUSD dailyTradeVolumeUSD totalTVL txCount updateAtBlock {id}  
    } 
  } 
}
```

#### BridgeTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'KSM'}], block_hash = hash)*
- LKSM (or LDOT) obtained from function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'Token': 'LKSM'}], block_hash = hash)*
- both mulitplied by KSM (or DOT) price for the specific block number

#### liquidStakingTVL
- KSM (or DOT) obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Homa',storage_function='TotalStakingBonded', block_hash = hash)*
- mulitplied by KSM (or DOT) price for the specific block number

#### lcdotTVL (Acala only)
- LCDOT obtained from the [py-substrate-interface](https://github.com/polkascan/py-substrate-interface) using function *substrate.query(module='Tokens',storage_function='TotalIssuance', params = [{'LiquidCrowdloan': 13}], block_hash = hash)*
- mulitplied by DOT price for the specific block number

#### stableCoinTVL
- sum of depositVolumeUSD from the *getLoansDailyCollateral_acala_loan* function in the [subscanr](https://github.com/rogerjbos/subscanr) package for R.  You can also use the [Karura-loan]https://explorer.subquery.network/subquery/AcalaNetwork/karura-loan) project hosted on Subquery Network with this query:
```
{ 
  dailyCollaterals { 
    nodes { 
      collateral {id} depositAmount debitAmount depositVolumeUSD debitVolumeUSD
      depositChangedUSD debitChangedUSD debitExchangeRate timestamp txCount 
    } 
  } 
}
```